<template>
  <div class="pdf-preview-model">
    <div class="pdf-preview">
      <div class="pdf-wrap" @click="close">
        <vue-pdf-embed @click.stop="" :source="state.source" annotation-layer text-layer :style="scale" class="vue-pdf-embed" :page="state.pageNum" />
      </div>
    </div>
    <div class="page-tool">
      <div class="page-tool-item" @click="lastPage">上一页</div>
      <div class="page-tool-item" @click="nextPage">下一页</div>
      <div class="page-tool-item">{{ state.pageNum }}/{{ state.numPages }}</div>
      <div class="page-tool-item" @click="pageZoomOut">放大</div>
      <div class="page-tool-item" @click="pageZoomIn">缩小</div>
    </div>
    <div class="closeBtn" @click="close">
      <CloseOutlined class="img-preview__close-icon"/>
    </div>
  </div>
</template>
<script setup>
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { reactive, ref, onMounted, computed } from 'vue';
  import VuePdfEmbed from 'vue-pdf-embed';
  import { createLoadingTask } from 'vue3-pdfjs';
  import { CloseOutlined } from '@ant-design/icons-vue';
  import 'vue-pdf-embed/src/styles/annotation-layer.css'
  import 'vue-pdf-embed/src/styles/text-layer.css'
  const props = defineProps({
    pdfUrl: {
      type: String,
      required: true,
    },
  });
  const emit= defineEmits(['closePdf'])
  let pdfUrl = ref('');
  const pageHeight = ref('100%');
  const [register, { closeModal }] = useModalInner((data) => {
    // pdfUrl.value=data.pdfUrl;
  });
  const state = reactive({
    source: props.pdfUrl, //预览pdf文件地址
    pageNum: 1, //当前页面
    scale: 1, // 缩放比例
    numPages: 0, // 总页数
  });
  const scale = computed(() => `transform:scale(${state.scale})`);
  function lastPage() {
    if (state.pageNum > 1) {
      state.pageNum -= 1;
    }
  }
  function nextPage() {
    if (state.pageNum < state.numPages) {
      state.pageNum += 1;
    }
  }
  function pageZoomOut() {
    if (state.scale < 2) {
      state.scale += 0.1;
      pageHeight.value = parseInt(pageHeight.value) + 5.0 + '%';
    }
  }
  function pageZoomIn() {
    if (state.scale > 1) {
      state.scale -= 0.1;
      pageHeight.value = parseInt(pageHeight.value) - 5.0 + '%';
    }
  }
  function close (){
    emit('closePdf');
  }

  onMounted(() => {
    const loadingTask = createLoadingTask(state.source);
    loadingTask.promise.then((pdf) => {
      state.numPages = pdf.numPages;
    });
  });
</script>
<style lang="css" scoped>
  .pdf-preview-model {
    position: fixed;
    height: 100vh;
    overflow: auto;
    width: 100vw;
    top: 0;
    left: 0;
    z-index: 99999;
    background: rgba(0, 0, 0, 0.5);
  }
  .pdf-preview {
    position: relative;
    /* width:100vw; */
    height: 100%;
    padding: 20px 0;
    box-sizing: border-box;
    /* background-color: #e9e9e9; */
    overflow: auto;
  }
  .pdf-wrap {   
    width:100%;
    height: 100%;
    overflow-y: visible;
  }
  .vue-pdf-embed {
    text-align: center;
    width:50%;
    min-height: 100vh;
    border: 1px solid #e5e5e5;
    box-sizing: border-box;
    overflow-y: auto;
    transform-origin: center top;
    position:relative;
    left:0;
    right: 0;
    margin: 0 auto;
  }

  .page-tool {
    position: absolute;
    bottom: 35px;
    padding-left: 15px;
    padding-right: 15px;
    display: flex;
    align-items: center;
    background: rgb(66, 66, 66);
    color: white;
    border-radius: 19px;
    z-index: 100;
    cursor: pointer;
    margin-left: 50%;
    transform: translateX(-50%);
  }
  .page-tool-item {
    padding: 8px 15px;
    padding-left: 10px;
    cursor: pointer;
  }
  .closeBtn {
    /* position: absolute;
    right: -15px;
    top: -10px;
    width: 60px;
    height: 60px;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    font-weight: bold;
    border-radius: 50%;
    cursor: pointer;     */





    position: absolute;
    top: -40px;
    right: -40px;
    width: 80px;
    height: 80px;
    overflow: hidden;
    color: #fff;
    cursor: pointer;
    background-color: rgba(0, 0, 0, 0.5);
    border-radius: 50%;
    transition: all 0.2s;
  }
  .img-preview__close-icon {
    position: absolute;
    top: 46px;
    left: 16px;
    font-size: 16px;
}
</style>
